package com.amc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.amc.constant.MerchantConstant;
import com.amc.dao.IMerchantDao;
import com.amc.entity.Merchant;
import com.amc.utils.DbUtil;

public class MerchantDaoImpl implements IMerchantDao {

	private SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

	@Override
	public Merchant queryByMerchantId(String merchantId) {
		Connection conn = DbUtil.getConnection();
		String sql = "SELECT * FROM a_merchant WHERE merchant_id =?";
		PreparedStatement ps = null;
		Merchant merchant = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, merchantId);
			rs = ps.executeQuery();

			while (rs.next()) {
				merchant = new Merchant();
				merchant.setMerchantId(rs.getString("merchant_id"));
				merchant.setMerchantName(rs.getString("merchant_name"));
				merchant.setMerchantAddress(rs.getString("merchant_address"));
				merchant.setMerchantPic(rs.getString("merchant_pic"));
				merchant.setMerchantIdcard(rs.getString("merchant_idcard"));
				merchant.setAuditStatus(rs.getInt("audit_status"));
				merchant.setIsBlack(rs.getInt("is_black"));
				merchant.setRemarks(rs.getString("remarks"));
				merchant.setLastApplyTime(Long.valueOf(rs.getString("last_apply_time")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, ps, null);
		}
		return merchant;
	}

	@Override
	public List<Merchant> query(String isBlack, String merchantName) {
		Connection conn = DbUtil.getConnection();
		String sql = "SELECT * FROM a_merchant where 1=1";
		if(isBlack != null && !"".equals(isBlack)){
			sql += " and is_black = " + isBlack;
		}
		if(merchantName != null && !"".equals(merchantName)){
			sql += " and merchant_name like '%" + merchantName +"%'";
		}
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Merchant> merchantList = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			while (rs.next()) {
				Merchant merchant = new Merchant();
				merchant.setMerchantId(rs.getString("merchant_id"));
				merchant.setMerchantName(rs.getString("merchant_name"));
				merchant.setMerchantAddress(rs.getString("merchant_address"));
				merchant.setMerchantPic(rs.getString("merchant_pic"));
				merchant.setMerchantIdcard(rs.getString("merchant_idcard"));
				merchant.setAuditStatus(rs.getInt("audit_status"));
				merchant.setIsBlack(rs.getInt("is_black"));
				merchant.setRemarks(rs.getString("remarks"));
				merchant.setLastApplyTime(Long.valueOf(rs.getString("LAST_APPLY_TIME")));
				merchantList.add(merchant);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, ps, null);
		}
		return merchantList;
	}

	@Override
	public boolean update(Merchant merchant) {
		boolean result = false;

		Connection conn = DbUtil.getConnection();
		String sql = "update a_merchant set merchant_name = ?,  merchant_address = ?, merchant_pic = ?,"
				+ "merchant_idcard = ? ,audit_status=? ,remarks = ?, is_black = ? ,LAST_APPLY_TIME = ? where merchant_id = ? ";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, merchant.getMerchantName());
			ps.setString(2, merchant.getMerchantAddress());
			ps.setString(3, merchant.getMerchantPic());
			ps.setString(4, merchant.getMerchantIdcard());
			ps.setInt(5, merchant.getAuditStatus());
			ps.setString(6, merchant.getRemarks());
			ps.setInt(7, merchant.getIsBlack());
			
			ps.setString(8, merchant.getLastApplyTime()+"");
			
			ps.setString(9, merchant.getMerchantId());

			result = (ps.executeUpdate() == -1) ? false : true;

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, ps, null);
		}

		return result;
	}

	@Override
	public boolean delete(String merchantId) {
		// TODO Auto-generated method stub
		boolean result = false;
		return result;
	}

	@Override
	public List<String> getMerchantIdList() {
		Connection conn = DbUtil.getConnection();
		String sql = "SELECT merchant_id FROM a_merchant where audit_status = " + MerchantConstant.AUDIT_STATUS_PASS +" and is_black = " + MerchantConstant.WHITE_LIST;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<String> merchantIdList = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			while (rs.next()) {
				merchantIdList.add(rs.getString(1));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, ps, null);
		}
		return merchantIdList;
	}

	@Override
	public boolean insert(Merchant merchant) {
		boolean result = false;
		Connection conn = DbUtil.getConnection();
		String sql = "insert into a_merchant (merchant_id, merchant_name ,  merchant_address, merchant_pic,"
				+ "merchant_idcard ,audit_status  , is_black , LAST_APPLY_TIME) values(?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, merchant.getMerchantId());
			ps.setString(2, merchant.getMerchantName());
			ps.setString(3, merchant.getMerchantAddress());
			ps.setString(4, merchant.getMerchantPic());
			ps.setString(5, merchant.getMerchantIdcard());
			ps.setInt(6, merchant.getAuditStatus());
			ps.setInt(7, merchant.getIsBlack());
			ps.setString(8, merchant.getLastApplyTime()+"");
			result = (ps.executeUpdate() == -1) ? false : true;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, ps, null);
		}
		return result;
	}
}
